// server side include file

<!--#INCLUDE file="ra_string.js" -->

var UA = RMAGetPropertyValue( "headers.User-Agent" );
var isIE = UA.bSearch( /MSIE/i );
var isNav = ! isIE ;
var isWin = UA.bSearch( /Win/ );
var isMac = UA.bSearch( /Mac/ );

function propToVarName( s )
{
	s = s.replace( /\./g, "$" );
	return s.replace( / /g, "_" );
}

function enumProps ( rootProp, enumFunc, maxDepth )
{
	if ( ! ( rootProp && enumFunc ) ) return ;

	// default maxDepth is zero, i.e. full recursion
	if ( enumProps.arguments.length < 3 ) maxDepth = 0 ;

	function _enumProps( rootProp, curDepth)
	{
		if ( maxDepth && ( curDepth > maxDepth ) ) return ;

		// if enumFunc returns a non-zero value that's a signal to abort the
		// enumeration
	    if( enumFunc( rootProp, RMAGetPropertyValue( rootProp ), curDepth ) ) return ;

		curDepth++ ;

	    var x = -1 ;
	    var subProp ;

	    while ( ( subProp = RMAGetNthProperty( rootProp, ++x ) ) )
	    {
	        _enumProps( subProp, curDepth );
	    }
		curDepth-- ;
	}

	_enumProps( rootProp, 0 );
}

// enumerates all properties which contain a subproperty == subProp
// search is case insensitive
function findSubProp ( root, subProp, callback )
{
	var re = new RegExp( "\\." + subProp + "\\.", "i" );

	function _enumFunc( prop, val, curDepth )
	{
		if ( re.test( '.' + prop + '.' ) )
			return callback( prop, val, curDepth );
	}
	enumProps( root, _enumFunc, 0 );
}

// returns the firt property whose baseProp == subProp and 
// whose value == srchVal
// search is case insensitive
function findSubPropVal ( root, subProp, srchVal )
{
	var matchProp = "" ;
	subProp = subProp.toLowerCase();
	srchVal = srchVal.toLowerCase();

	function _enumFunc( prop, val, curDepth )
	{
		if ( ( prop.rightProp().toLowerCase() == subProp ) && 
			 ( val.toString().toLowerCase() == srchVal ) )
		{
			matchProp = prop ;
			return 1 ;
		}
	}
	enumProps( root, _enumFunc, 0 );
	return matchProp ;
}

function emitSetVar( varName, newVal, isConstant )
{
	var s = varName + "=" + (( newVal == null || newVal.toString() == "null" ) ? "null" : (isConstant ? newVal : qVar( newVal ))) + ";\n" ;
	RMAOutput( s );
}

function emitVarDecl( varName /*, optional initVal, optional isConstant */ )
{
	var s = "var " + varName + " " ;

	if ( arguments.length > 1 )
	{
		var initVal = arguments[ 1 ];

		var isConstant = false ;
		if ( arguments.length > 2 ) 
			isConstant = arguments[ 2 ];

		s += "=" + ((initVal == null || initVal.toString() == "null") ? "null" : 
						(isConstant ? initVal : qVar(initVal)));
	}
	s += " ;\n" ;

	RMAOutput( s );
}

function emitArrayProp( arrayName, prop, val )
{
	var s = arrayName + "[" + qVar( prop ) + "]=" ;
	s += (( val == null ) ? val : qVar(val)) + ";\n" ;

	RMAOutput( s );
}

function emitPropsToJSArray( rootProp, arrayName, maxDepth, needScript, fSendClearPasswords )
{
	if ( ! rootProp ) return ;
	if ( ! arrayName ) arrayName = propToVarName( rootProp );

	var s = "" ;
	var reDot = new RegExp( "\\.", "g" );
	var reEmptyList1 = new RegExp( "\\.\\s*\\." );
	var reEmptyList2 = new RegExp( "\\.\\s*$" );

	function _enumFunc( prop, val, curDepth )
	{
		if ( maxDepth && ( curDepth > maxDepth ) ) return ;

		var arrayNameTmp = arrayName ;

		// check for empty list names and skip em' and all thier children!
		// ie: "config.rules..SomeRuleProperty", "config.rules.  .SomeRuleProperty", "config.rules."
		if ( prop.bSearch( reEmptyList1 ) || 
		     prop.bSearch( reEmptyList2 ) )
			return ;

		//chop off rootProp
		var subArrays = prop.substr( rootProp.length + 1);

		if ( subArrays )
		{
			//prepare for quoting
			subArrays = qEsc( subArrays );

			// chop up into subarrays and quote 
			arrayNameTmp += "['" + subArrays.replace( reDot, "']['" ) + "']" ;
		}

		// if this property is a list of other properties then it's value 
		// is another array (but only if we can recurse futher (check curDepth)
		if ( RMAGetNthProperty( prop, 0 ) )
		{
			s += arrayNameTmp + "=new Array();\n" ;
		}
		else
		{
			//HACK don't send passwords down the wire
			if ( (!fSendClearPasswords) && (prop.rightProp().bSearch( /password/i )) ) 
			{
 				val = '********' ;
			}

			s += arrayNameTmp + "=" + (( val == null ) ? val : qVar(val)) + ";\n" ;
		}
	}

	enumProps( rootProp, _enumFunc, maxDepth );

	// if the no properties were found, we want an empty array instead of null
	if ( s == (arrayName + "=null;\n") )
		s = arrayName + "=new Array();\n" ;

	if ( needScript ) 
		s = "<script language='javascript'>\n" + s + "</script>\n";

	RMAOutput( s );
}

function emitServArrayToClientArray( array, clientArrayName, maxDepth, needScript )
{
	if ( ! clientArrayName ) return ;

	var s = "var " + clientArrayName + " = new Array();\n" ;

	function _enumServArray( a, arrayName, curDepth )
	{
		if ( maxDepth && ( curDepth >= maxDepth ) ) return ;

		for ( var i in a )
		{
			if ( (a[ i ] != null) && (typeof( a[ i ] ) == "object") )
			{
				var newName = arrayName + "[" + qVar( i ) + "]" ;
				s += newName + " = new Array();\n" ;
				_enumServArray( a[ i ], newName, curDepth + 1 );
			}
			else
			{
				s += arrayName + "[" + qVar( i ) + "] = " + 
					(( a[i] == null ) ? a[i] : qVar(a[i])) + ";\n" ;
			}
		}
	}

	if ( array )
		_enumServArray( array, clientArrayName, 0 );

	if ( needScript ) 
		s = "<script language='javascript'>\n" + s + "</script>\n";

	RMAOutput( s );
}

function emitFSMountListNames ()
{
	var aFSMountLists = new Array();
	function enumFSMountCallback ( prop, value, curDepth )
	{
		if ( curDepth == 1 )
			aFSMountLists[ prop.rightProp( -2 ) ] = RMAGetPropertyValue( prop + ".MountPoint" );	
	}
	enumProps( "config.FSMount", enumFSMountCallback, 1 );
	emitServArrayToClientArray( aFSMountLists, "FSMountListNames", 0 );
}


